<?php

class Zen_Bootstrap_DynTree extends Zend_Application_Module_Bootstrap {
	
	protected $_model = null;
	
	public function _initBaseRoutes() {
		$front = Zend_Controller_Front::getInstance ();
		$router = $front->getRouter ();
		$routes = $this->_getRoutes();
		$router->addRoutes ( $routes );
		exit;
	}
	
	private function _getCahce () {
		$cnf=Zend_Registry::get('cnf');
		$cache_dir = $cnf->cache_dir."route";
		if(Zend_Registry::isRegistered('cacheManager')){
            $manager=Zend_Registry::get('cacheManager');
        }else{
           	$manager = new Zend_Cache_Manager();
           	Zend_Registry::set('cacheManager',$manager);
        }
        if (!$manager->hasCacheTemplate('routeCache')) {
        	$routeCache = array(
			    'frontend' => array(
				    'name' => 'Core',
			        'options' => array(
		            	'lifetime' => NULL,
		            	'automatic_serialization' => true
					)
				),
		    	'backend' => array(
		        	'name' => 'File',
		        	'options' => array(
		            	'cache_dir' => $cache_dir,
		            	'file_name_prefix' => 'route',
						'cache_file_umask'=> 0777,
						'hashed_directory_level' => 2,
						'hashed_directory_umask' => 0777,
		            )
		        )
	       	);
			if (!file_exists ( $cache_dir)) {
	        	mkdir ( $cache_dir, 0777, true );
	        }
        	$manager->setCacheTemplate('routeCache', $routeCache);
        }
        $cache = $manager->getCache ( 'routeCache' );
        return $cache;
	}
	
	private function _getMaxLevel () {
		$module_name = strtolower($this->getModuleName());
		if ($this->_model_name) {
			$tree_model = $this->getModel();
		} else {
			$model_name = ucfirst($module_name).'_Model_Tree';
			$tree_model = new $model_name();
		}
			
		$list_model = new Zen_List($tree_model->getTableName());
		$row = $list_model->select()->setColumns( array('max_level' => new Zend_Db_Expr('MAX(level)') ))->getOne();
		$max_level = $row['max_level'];
		return $max_level;
	}
	
	private function _getRoutes () {
		$module_name = strtolower($this->getModuleName());
        $cache = $this->_getCahce();
		$routes = $cache->load ( $module_name.'dyn_tree_routes' );
		if (! is_array($routes)) {
			$routes = array();
			$max_level = $this->_getMaxLevel();
			if ($max_level) {
				for ( $i = 0; $i < $max_level; $i++ ) {
					$route_param = array ('module' => $module_name, 'controller' => 'index', 'action' => 'index', );
					$route_str = $module_name.'/';
					for ( $j = 0; $j <= $i; $j++) {
						$route_str .= ':level'.$j.'/';
						$route_param['level'.$j] = false;
					}
					$route_str .= "*";
					$route = new Zend_Controller_Router_Route ( $route_str, $route_param );
					$key = $module_name . $i;
					$routes[$key] = $route;
				}
			}
			$cache->save ( $routes, $module_name.'dyn_tree_routes');
		}
		return $routes;
	}
	
	public function setModel($value) {
		$this->_model = $value;
		return $this;
	}
	
	public function getModel() {
		return $this->_model;
	}
	
}
